#ifndef __MUTEX_H__
#define __MUTEX_H__

#include <pthread.h>
#include <errno.h>

/*---------------------------------------------------------------------*/

class CMutex {
public:
    //initialize mutex
    CMutex() {
        pthread_mutex_init(&mMutex, NULL);
    }

    //destory ,making sure mutex is unlocked
    ~CMutex() {
        int rc = pthread_mutex_destroy(&mMutex);
        if (rc == EBUSY) {
            Unlock();
            pthread_mutex_destroy(&mMutex);
        }
    }

    // lock the mutex variable
    void Lock(void) {
        pthread_mutex_lock(&mMutex);
    }

    // unlock the mutex variable
    void Unlock(void) {
        pthread_mutex_unlock(&mMutex);
    }

protected:
    pthread_mutex_t mMutex;
};//end class Mutex

#endif

